package com.example.vote.service.impl;

import com.example.vote.entity.TpItems;
import com.example.vote.entity.TpVote;
import com.example.vote.mapper.TpVoteMapper;
import com.example.vote.service.ITpItemsService;
import com.example.vote.service.ITpVoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 投票系统-题目表-2 服务实现类
 * </p>
 *
 * @author 廖老师
 * @since 2023-12-04
 */
@Service
public class TpVoteServiceImpl extends ServiceImpl<TpVoteMapper, TpVote> implements ITpVoteService {

    @Resource
    ITpItemsService itemsService;

    @Override
    @Transactional
    public void commit(TpVote vote) {
        this.save(vote);
        for (TpItems item : vote.getItems()) {
            // mybatis plus 可以获取数据库生成的主键值
            item.setVid(vote.getId());
            itemsService.save(item);
        }
    }
}
